package fu.k4c.vTeam.ViewDetails;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import fu.k4c.vTeam.R;
import fu.k4c.vTeam.Main.BaseActivity;
import fu.k4c.vTeam.Object.Favorite;
import fu.k4c.vTeam.Object.LikeAndVisit;
import fu.k4c.vTeam.Object.Comment;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class InfoEventActivity extends BaseActivity {
	Boolean visted;
	TextView tv_event_visit_number;
	String email;
	String avatar;
	String PlaceID;
	private DisplayImageOptions options;
	Bundle data;
	int visitnumber;
	int CURRENT_TAG = 0;
	static final int TAG_GUEST = 0;
	static final int TAG_USER = 1;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_view_event_details);
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.stub_image)
				.showImageForEmptyUri(R.drawable.image_for_empty_url)
				.cacheInMemory().cacheOnDisc()
				.displayer(new RoundedBitmapDisplayer(20)).build();
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
				.permitAll().build();
		StrictMode.setThreadPolicy(policy);
		TextView tv_event_name = (TextView) findViewById(R.id.tv_view_tab_event_details_placename);
		TextView tv_event_address = (TextView) findViewById(R.id.tv_view_tab_event_details_address);
		TextView tv_event_tel = (TextView) findViewById(R.id.tv_view_tab_event_details_tel_number);
		TextView tv_event_begin = (TextView) findViewById(R.id.tv_view_tab_event_details_begin);
		TextView tv_event_end = (TextView) findViewById(R.id.tv_view_tab_event_details_endate);
		tv_event_visit_number = (TextView) findViewById(R.id.tv_view_tab_event_details_person_visit);
		TextView tv_event_create_by = (TextView) findViewById(R.id.tv_view_tab_event_details_create_by);
		TextView tv_event_description = (TextView) findViewById(R.id.tv_view_tab_event_details_description);
		TextView tv_event_view = (TextView) findViewById(R.id.tv_view_tab_event_details_view);
		ImageView img_avatar = (ImageView) findViewById(R.id.img_view_tab_event_details_avatar);

		try {
			data = getIntent().getExtras();
			if (data != null) {
				visitnumber = data.getInt("visitnumber");
				email = data.getString("email");
				avatar = data.getString("avatar");
				if (email.equals("Guest")) {
					CURRENT_TAG = TAG_GUEST;
				} else {
					CURRENT_TAG = TAG_USER;
				}
				tv_event_name.setText(data.getString("placename"));
				tv_event_address.setText(data.getString("address"));
				if (data.getString("placetel") == null) {
					tv_event_tel.setText("Số điện thoại: N\\A");
				} else
					tv_event_tel.setText("Số điện thoại:"
							+ data.getString("placetel"));
				tv_event_begin.setText("Bắt đầu: " + data.getString("begin"));
				tv_event_end.setText("Kết thúc: " + data.getString("end"));
				tv_event_visit_number.setText("Đã có "
						+ data.getInt("visitnumber")
						+ " người tham gia sự kiện này");
				tv_event_description.setText(data.getString("des"));
				tv_event_create_by.setText(data.getString("createby"));
				tv_event_view.setText("Đã có " + data.getString("views")
						+ " lượt xem");
				PlaceID = data.getString("IDofItemRevision");
				visted = data.getBoolean("visited");
				// Toast.makeText(getApplicationContext(), ""+liked +","+visted,
				// Toast.LENGTH_LONG).show();
			}
		} catch (Exception e) {
			// TODO: handle exception
			Toast.makeText(getApplicationContext(), e.toString(),
					Toast.LENGTH_LONG).show();
		}
		if (avatar.equals("null")) {
			imageLoader.init(ImageLoaderConfiguration
					.createDefault(getBaseContext()));
			imageLoader
					.displayImage(
							"http://i168.photobucket.com/albums/u194/trunga1k17/stub_image_zps55175a5c.png",
							img_avatar, options);

		} else {
			if (avatar.toLowerCase().indexOf("http://") < 0) {
				avatar = getString(R.string.connectionString) + avatar;

			}
			imageLoader.init(ImageLoaderConfiguration
					.createDefault(getBaseContext()));
			imageLoader.displayImage(avatar, img_avatar, options);
		}
		if (visted) {
			Button btn_visited = (Button) findViewById(R.id.btn_view_tab_event_details_visit);
			btn_visited.setText("Bạn sẽ tham gia");
			btn_visited.setBackgroundResource(R.drawable.btn_checkin);
		}
		final Button btn_visit = (Button) findViewById(R.id.btn_view_tab_event_details_visit);
		btn_visit.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (CURRENT_TAG == 0) {
					Toast.makeText(
							getApplicationContext(),
							"Bạn phải đăng nhập để có thể sử dụng chức năng này",
							Toast.LENGTH_LONG).show();
				} else {
					if (visted) {
						Toast.makeText(getApplicationContext(),
								"Bạn đã đánh dấu tham gia sự kiện này",
								Toast.LENGTH_LONG).show();
					} else {
						try {
							SchemeRegistry registry = new SchemeRegistry();
							registry.register(new Scheme("http",
									PlainSocketFactory.getSocketFactory(), 80));
							HttpParams httpParams = new BasicHttpParams();
							ThreadSafeClientConnManager connMgr = new ThreadSafeClientConnManager(
									httpParams, registry);
							HttpClient client = new DefaultHttpClient(connMgr,
									httpParams);
							HttpPost post = new HttpPost(
									getString(R.string.connectionString)
											+ "vLocationService/checkin");
							post.setHeader("Accept", "application/json");
							post.setHeader("Content-type", "application/json");
							HashMap<String, Object> postParameters = new HashMap<String, Object>();
							postParameters.put("UserName", email);
							postParameters.put("PlaceId", PlaceID);
							Gson gson = new Gson();
							String jsonParameters = gson.toJson(postParameters);
							post.setEntity(new StringEntity(jsonParameters));
							HttpResponse response = client.execute(post);
							HttpEntity entity = response.getEntity();
							String jsonresult = EntityUtils.toString(entity);
							Favorite fvr = gson.fromJson(jsonresult,
									Favorite.class);
							if (fvr.getStatus().equals("true")) {
								btn_visit.setText("Bạn sẽ tham gia");
								visted = true;
								visitnumber += 1;
								tv_event_visit_number.setText("Đã có "
										+ visitnumber
										+ " người tham gia sự kiện này");
								btn_visit
										.setBackgroundResource(R.drawable.btn_checkin);
							} else {
								Toast.makeText(getApplicationContext(),
										"" + jsonresult, Toast.LENGTH_LONG)
										.show();
							}
						} catch (Exception e) {
							// TODO: handle exception
							Toast.makeText(getApplicationContext(),
									e.toString(), Toast.LENGTH_LONG).show();

						}
					}
				}
			}
		});
	}

}
